package com.movecar.ui;

/**
 * 浮云主题详情-瀑布流
 * 
 * @author Zhao.JQ
 * 
 */
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.movecar.R;
import com.movecar.listview.WaterFallListView;
import com.movecar.listview.WaterFallListView.IXListViewListener;
import com.movecar.model.ClubBean;
import com.movecar.model.GroupBean;
import com.movecar.model.ThemeContentBean;
import com.movecar.model.ThemeDetailBean;
import com.movecar.utils.Alert;
import com.movecar.utils.AppConfig;
import com.movecar.utils.Converter;
import com.movecar.utils.DataStore;
import com.movecar.utils.HttpPostRequestAsyncTask;
import com.movecar.utils.RequestResultCallback;
import com.movecar.utils.imageloader.Constants;
import com.movecar.utils.imageloader.Utils;
import com.movecar.widget.CustomProgressDialog;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.rockerhieu.emojicon.EmojiconTextView;

public class ThemeDetailWaterFallActivity extends FragmentActivity implements IXListViewListener, OnClickListener {

	private CustomProgressDialog progressDialog = null;
	private WaterFallListView mAdapterView = null;
	protected WaterFallListView mXList;
	protected WaterFallListView mXListClub;
	private StaggeredAdapter mAdapter = null;
	private String pageNo = "0";
	private ImageLoader imageLoader = null;
	private LayoutInflater inflater;
	private LayoutInflater inflater_group;
	private List<ThemeContentBean> mDataList;
	private List<ThemeContentBean> resultList;
	private List<GroupBean> mThemeDataList;
	private List<GroupBean> resultThemeList = new ArrayList<GroupBean>();
	private ThemeDetailBean mThemeDetailBean;
	private List<ClubBean> mClubDataList;
	private List<ClubBean> resultClubList = new ArrayList<ClubBean>();
	private boolean flgMore = false;
	private String ThemeId, detailLogo, isRelate, validMessageCount, isRed;
	private Button btn_kfxiang, btn_qjhui, joined, btn_kanfxiang, btn_qdli;
	private DataAdapter dataAdapter;
	private clubDataAdapter clubDataAdapter;
	private RelativeLayout right_btn_chance, right_btn_go_club;
	private TextView order_num_bg;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		imageLoader = ImageLoader.getInstance();
		setContentView(R.layout.theme_detail_waterfall);
		// 分享列表
		mAdapterView = (WaterFallListView) findViewById(R.id.list);
		mAdapterView.setPullLoadEnable(true);
		mAdapterView.setPullRefreshEnable(true);
		mAdapterView.setXListViewListener(this);
		// 活动列表
		mXList = (WaterFallListView) findViewById(R.id.mListView);
		mXList.setPullRefreshEnable(true);
		mXList.setPullLoadEnable(true);
		mXList.setXListViewListener(this);

		// 商家列表

		mXListClub = (WaterFallListView) findViewById(R.id.mClubListView);
		mXListClub.setPullRefreshEnable(true);
		mXListClub.setPullLoadEnable(true);
		mXListClub.setXListViewListener(this);

		ThemeId = getIntent().getStringExtra("ThemeId");
		detailLogo = getIntent().getStringExtra("detailLogo");
		isRelate = getIntent().getStringExtra("isRelate");

		inflater = LayoutInflater.from(this);
		View headview = inflater.inflate(R.layout.theme_advertisement, null);
		headview.setBackgroundResource(R.color.main_background);

		headview.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(ThemeDetailWaterFallActivity.this, ThemeMatchDetailActivity.class);
				i.putExtra("ThemeId", ThemeId);
				i.putExtra("isRelate", isRelate);
				startActivity(i);

			}
		});
		mAdapterView.addHeaderView(headview);
		mAdapter = new StaggeredAdapter(this, mAdapterView);
		mAdapterView.setAdapter(mAdapter);

		ImageView img = (ImageView) headview.findViewById(R.id.ad);

		imageLoader.displayImage(AppConfig.HOST + detailLogo, img, Constants.image_display_options,
				AppConfig.animateListener);

		inflater_group = LayoutInflater.from(this);
		View group_headview = inflater_group.inflate(R.layout.theme_group_advertisement, null);
		group_headview.setBackgroundResource(R.color.main_background);

		group_headview.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(ThemeDetailWaterFallActivity.this, ThemeMatchDetailActivity.class);
				i.putExtra("ThemeId", ThemeId);
				i.putExtra("isRelate", isRelate);
				startActivity(i);

			}
		});
		mXList.addHeaderView(group_headview);
		mXListClub.addHeaderView(group_headview);
		dataAdapter = new DataAdapter(this);
		mXList.setAdapter(dataAdapter);

		clubDataAdapter = new clubDataAdapter(ThemeDetailWaterFallActivity.this);
		mXListClub.setAdapter(clubDataAdapter);
		ImageView img_ad = (ImageView) group_headview.findViewById(R.id.ad);

		imageLoader.displayImage(AppConfig.HOST + detailLogo, img_ad, Constants.image_display_options,
				AppConfig.animateListener);

		// TextView title = (TextView) findViewById(R.id.title);
		// title.setText("hi主题");
		order_num_bg = (TextView) findViewById(R.id.order_num_bg);

		Button btn_left = (Button) findViewById(R.id.btn_left);
		btn_left.setVisibility(View.VISIBLE);
		btn_left.setOnClickListener(this);
		// 右上角按钮
		right_btn_chance = (RelativeLayout) findViewById(R.id.right_btn_chance);
		right_btn_go_club = (RelativeLayout) findViewById(R.id.right_btn_go_club);
		if ("0".equals(isRelate)) {
			right_btn_go_club.setVisibility(View.VISIBLE);
			right_btn_chance.setVisibility(View.GONE);
		} else {
			right_btn_go_club.setVisibility(View.GONE);
			right_btn_chance.setVisibility(View.VISIBLE);
		}

		btn_kfxiang = (Button) findViewById(R.id.btn_kfxiang);
		btn_qjhui = (Button) findViewById(R.id.btn_qjhui);
		btn_kfxiang.setOnClickListener(this);
		btn_kanfxiang = (Button) findViewById(R.id.btn_kanfxiang);
		btn_qdli = (Button) findViewById(R.id.btn_qdli);
		btn_kanfxiang.setOnClickListener(this);
		joined = (Button) findViewById(R.id.joined);
		joined.setOnClickListener(this);

		startProgressDialog();
		getThemeContent();

		// imageLoader.init(Constants.imageLoaderConfiguration(this));

	}

	public class StaggeredAdapter extends BaseAdapter {
		private Context mContext;
		private LinkedList<ThemeContentBean> mInfos;

		public StaggeredAdapter(Context context, WaterFallListView xListView) {
			mContext = context;
			mInfos = new LinkedList<ThemeContentBean>();
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {

			ViewHolder holder;
			final ThemeContentBean ThemeContentBean = mInfos.get(position);

			if (convertView == null) {
				LayoutInflater layoutInflator = LayoutInflater.from(parent.getContext());
				convertView = layoutInflator.inflate(R.layout.theme_detail_waterfall_item, null);
				holder = new ViewHolder();
				holder.user_info = (RelativeLayout) convertView.findViewById(R.id.user_info);
				holder.check_do = (RelativeLayout) convertView.findViewById(R.id.check_do);
				holder.zan_relative = (RelativeLayout) convertView.findViewById(R.id.zan_relative);
				holder.message_relative = (RelativeLayout) convertView.findViewById(R.id.message_relative);
				holder.news_pic = (ImageView) convertView.findViewById(R.id.news_pic);
				holder.user_img = (ImageView) convertView.findViewById(R.id.user_img);
				holder.user_name = (TextView) convertView.findViewById(R.id.user_name);
				holder.news_title = (EmojiconTextView) convertView.findViewById(R.id.news_title);
				holder.news_title.setEmojiconSize(Converter.dp2pixel(ThemeDetailWaterFallActivity.this, 20));
				holder.zan_text = (TextView) convertView.findViewById(R.id.zan_text);
				holder.message_text = (TextView) convertView.findViewById(R.id.message_text);
				holder.zan = (Button) convertView.findViewById(R.id.zan);
				holder.message = (Button) convertView.findViewById(R.id.message);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			imageLoader.displayImage(AppConfig.HOST + ThemeContentBean.getLogo(), holder.news_pic,
					Constants.image_display_options, AppConfig.animateListener);
			holder.news_title.setText(ThemeContentBean.getContent());
			holder.check_do.setVisibility(View.VISIBLE);
			holder.user_info.setVisibility(View.VISIBLE);
			if (ThemeContentBean.getIcon().contains("default")) {
				imageLoader.displayImage(AppConfig.HOST + ThemeContentBean.getIcon(), holder.user_img,
						Constants.image_display_options, AppConfig.animateListener);
			} else {
				imageLoader.displayImage(AppConfig.HOST + ThemeContentBean.getIcon(), holder.user_img,
						Constants.image_display_options_conner, AppConfig.animateListener);
			}
			holder.user_img.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (!ThemeContentBean.getShareUserId().equals(DataStore.getInstance().getUserId())) {
						Intent intent = new Intent().setClass(ThemeDetailWaterFallActivity.this,
								FriendPersonalInfActivity.class);
						intent.putExtra("shareUserId", ThemeContentBean.getShareUserId());
						startActivity(intent);
					} else {

						Intent intent = new Intent().setClass(ThemeDetailWaterFallActivity.this,
								PersonalInfActivity.class);
						startActivity(intent);

					}

				}
			});
			holder.user_name.setText(ThemeContentBean.getNickname());
			holder.zan_text.setText(ThemeContentBean.getPraiseCount());
			holder.message_text.setText(ThemeContentBean.getCommentCount());
			if ("0".equals(ThemeContentBean.getIsPraise())) {
				holder.zan.setBackgroundResource(R.drawable.zan);
			} else {
				holder.zan.setBackgroundResource(R.drawable.zan_click);

			}

			holder.message.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent i = new Intent(ThemeDetailWaterFallActivity.this, AddShareDiscussActivity.class);
					i.putExtra("position", position);
					i.putExtra("shareId", ThemeContentBean.getShareId());
					startActivityForResult(i, 0);

				}
			});
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent i = new Intent(ThemeDetailWaterFallActivity.this, AddShareDiscussActivity.class);
					i.putExtra("position", position);
					i.putExtra("shareId", ThemeContentBean.getShareId());
					startActivityForResult(i, 0);
				}

			});
			return convertView;
		}

		class ViewHolder {
			ImageView user_img;
			TextView user_name, zan_text, message_text;
			EmojiconTextView news_title;
			ImageView news_pic;
			Button zan, message;
			RelativeLayout user_info, check_do, zan_relative, message_relative;

		}

		@Override
		public int getCount() {
			return mInfos.size();
		}

		@Override
		public Object getItem(int arg0) {
			return mInfos.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		public void addItemLast(List<ThemeContentBean> datas) {
			mInfos.addAll(datas);
		}

		public void addItemTop(List<ThemeContentBean> datas) {
			mInfos.clear();
			for (ThemeContentBean info : datas) {
				mInfos.addLast(info);
			}
		}
	}

	@SuppressWarnings("unchecked")
	private void getThemeContent() {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		// 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "themeContentNew1"));
		// 添加要传递的参数

		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		params.add(new BasicNameValuePair("pmId", ThemeId));
		params.add(new BasicNameValuePair("pageNo", pageNo));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {

				stopProgressDialog();

				Gson gson = new Gson();
				String result = (String) object;

				mThemeDetailBean = gson.fromJson(result, ThemeDetailBean.class);

				if (mThemeDetailBean != null) {
					mDataList = mThemeDetailBean.getList();
					validMessageCount = mThemeDetailBean.getValidMessageCount();
					isRed = mThemeDetailBean.getIsRed();
					if (!"0".equals(validMessageCount)) {
						order_num_bg.setBackgroundResource(R.drawable.new_message_num);
						order_num_bg.setText(validMessageCount);
					}

					resultList = new ArrayList<ThemeContentBean>();
					if (flgMore) {
						resultList.clear();
						if (mDataList.size() == 0) {
							Alert.warn(ThemeDetailWaterFallActivity.this, "已经加载全部");
							if (!"1".equals(pageNo)) {
								pageNo = String.valueOf(Integer.parseInt(pageNo) - 1);
							} else {
								pageNo = "0";
							}
							resetXList();
						} else {
							resultList.clear();

							for (ThemeContentBean bean : mDataList) {
								resultList.add(bean);
							}
							mAdapter.addItemLast(resultList);
							resetXList();

						}
					} else {

						if (mDataList.size() == 0) {
							Alert.warn(ThemeDetailWaterFallActivity.this, "目前无内容");

							resetXList();
						}
						resultList.clear();

						for (ThemeContentBean bean : mDataList) {
							resultList.add(bean);
						}
						mAdapter.addItemTop(resultList);
						// dataAdapter.notifyDataSetChanged();

						resetXList();
					}

				} else {
					Alert.warn(ThemeDetailWaterFallActivity.this, "系统异常");
					resetXList();
				}

			}

			@Override
			public void onFail(String exception) {
				stopProgressDialog();
				Alert.warn(ThemeDetailWaterFallActivity.this, "与服务器连接失败，请检查网络");
				resetXList();

			}
		}, DataStore.class, AppConfig.USER).execute(params);
	}

	protected void resetXList() {

		mAdapterView.setRefreshTime(new SimpleDateFormat("H:mm", Locale.CHINA).format(new Date()));
		mAdapterView.stopRefresh();
		mAdapterView.stopLoadMore();
		mXList.setRefreshTime(new SimpleDateFormat("H:mm", Locale.CHINA).format(new Date()));
		mXList.stopRefresh();
		mXList.stopLoadMore();
		mXListClub.setRefreshTime(new SimpleDateFormat("H:mm", Locale.CHINA).format(new Date()));
		mXListClub.stopRefresh();
		mXListClub.stopLoadMore();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {

				if (data.getStringExtra("zanCount") == null && !data.getBooleanExtra("delete", false)
						&& data.getBooleanExtra("isreplyed", false)) {
					int position = data.getIntExtra("position", 0);
					Object object = mAdapter.getItem(position);
					if (object instanceof ThemeContentBean) {
						ThemeContentBean themeContentBean = (ThemeContentBean) object;
						int commentCount = Integer.parseInt(themeContentBean.getCommentCount());

						commentCount = commentCount + 1;
						themeContentBean.setCommentCount(commentCount + "");

						mAdapter.notifyDataSetChanged();
					}
				} else if (data.getStringExtra("zanCount") != null && !data.getBooleanExtra("delete", false)
						&& data.getBooleanExtra("isreplyed", false)) {
					int position = data.getIntExtra("position", 0);
					Object object = mAdapter.getItem(position);
					if (object instanceof ThemeContentBean) {
						ThemeContentBean themeContentBean = (ThemeContentBean) object;
						if (Integer.parseInt(themeContentBean.getPraiseCount())
								- Integer.parseInt(data.getStringExtra("zanCount")) > 0) {
							themeContentBean.setIsPraise("0");
						} else if (Integer.parseInt(themeContentBean.getPraiseCount())
								- Integer.parseInt(data.getStringExtra("zanCount")) < 0) {
							themeContentBean.setIsPraise("1");
						} else {

						}
						int commentCount = Integer.parseInt(themeContentBean.getCommentCount());

						commentCount = commentCount + 1;
						themeContentBean.setCommentCount(commentCount + "");

						themeContentBean.setPraiseCount(data.getStringExtra("zanCount"));

						mAdapter.notifyDataSetChanged();
					}

				} else if (data.getStringExtra("zanCount") != null && !data.getBooleanExtra("delete", false)
						&& !data.getBooleanExtra("isreplyed", false)) {
					int position = data.getIntExtra("position", 0);
					Object object = mAdapter.getItem(position);
					if (object instanceof ThemeContentBean) {
						ThemeContentBean themeContentBean = (ThemeContentBean) object;
						if (Integer.parseInt(themeContentBean.getPraiseCount())
								- Integer.parseInt(data.getStringExtra("zanCount")) > 0) {
							themeContentBean.setIsPraise("0");
						} else if (Integer.parseInt(themeContentBean.getPraiseCount())
								- Integer.parseInt(data.getStringExtra("zanCount")) < 0) {
							themeContentBean.setIsPraise("1");
						} else {

						}
						themeContentBean.setPraiseCount(data.getStringExtra("zanCount"));

						mAdapter.notifyDataSetChanged();
					}
				}

			}
		}
	}

	@SuppressWarnings("unchecked")
	private void getThemeData() {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); // 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "queryPmMessageListNew1")); // 添加要传递的参数
		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		params.add(new BasicNameValuePair("pmId", ThemeId));
		params.add(new BasicNameValuePair("pageNo", pageNo));

		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {

				stopProgressDialog();
				Type type = new TypeToken<List<GroupBean>>() {
				}.getType();
				Gson gson = new Gson();
				String result = (String) object;

				try {

					mThemeDataList = gson.fromJson(result, type);
				} catch (Exception e) {
					return;
				}
				if (mThemeDataList != null) {
					mXList.setVisibility(View.VISIBLE);

					if (flgMore) {
						if (mThemeDataList.size() == 0) {
							Toast.makeText(getApplicationContext(), "目前没有更多活动", Toast.LENGTH_SHORT).show();
							if (!"1".equals(pageNo)) {
								pageNo = String.valueOf(Integer.parseInt(pageNo) - 1);
							} else {
								pageNo = "0";
							}
							resetXList();
						} else {

							resultThemeList.clear();

							for (GroupBean bean : mThemeDataList) {
								resultThemeList.add(bean);
							}
							dataAdapter.addItemLast(resultThemeList);
							resetXList();

						}
					} else {
						resultThemeList.clear();
						if (mThemeDataList.size() == 0) {

							Toast.makeText(getApplicationContext(), "目前没有抢机会的活动！", Toast.LENGTH_SHORT).show();

							resetXList();
						}
						for (GroupBean bean : mThemeDataList) {
							resultThemeList.add(bean);

						}
						dataAdapter.addItemTop(resultThemeList);
						dataAdapter.notifyDataSetChanged();
						resetXList();
					}
				} else {
					Toast.makeText(getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
				}

			}

			@Override
			public void onFail(String exception) {

				Alert.error(ThemeDetailWaterFallActivity.this,"与服务器连接失败，请检查网络");

			}
		}, GroupBean.class, AppConfig.GROUP).execute(params);
	}

	class DataAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		private ImageLoader imageLoader = null;
		private LinkedList<GroupBean> resultList;

		DataAdapter(Activity activity) {

			inflater = LayoutInflater.from(activity);
			imageLoader = ImageLoader.getInstance();
			resultList = new LinkedList<GroupBean>();
		}

		public void addItemLast(List<GroupBean> datas) {
			resultList.addAll(datas);
		}

		public void addItemTop(List<GroupBean> datas) {
			resultList.clear();
			for (GroupBean info : datas) {
				resultList.add(info);
			}
		}

		@Override
		public int getCount() {

			return resultList.size();
		}

		@Override
		public Object getItem(int arg0) {

			return resultList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {

			return arg0;
		}

		@Override
		public View getView(final int position, View view, ViewGroup arg2) {
			final ViewHolder viewHolder;
			if (view == null) {
				viewHolder = new ViewHolder();
				view = inflater.inflate(R.layout.group_item, null);
				viewHolder.group_img = (ImageView) view.findViewById(R.id.group_img);
				viewHolder.group_title = (TextView) view.findViewById(R.id.group_title);
				viewHolder.joined_num = (TextView) view.findViewById(R.id.group_joined_num);
				viewHolder.limit_num = (TextView) view.findViewById(R.id.group_limit_num);
				viewHolder.group_time = (TextView) view.findViewById(R.id.group_time);
				viewHolder.feedbackCoin = (TextView) view.findViewById(R.id.feedbackCoin);
				viewHolder.group_description = (TextView) view.findViewById(R.id.group_description);
				viewHolder.berserk = (ImageView) view.findViewById(R.id.berserk);
				viewHolder.invitation = (ImageView) view.findViewById(R.id.invitation);
				viewHolder.group_type = (ImageView) view.findViewById(R.id.group_type);
				viewHolder.make_an_appointment = (TextView) view.findViewById(R.id.make_an_appointment);
				viewHolder.rmb = (TextView) view.findViewById(R.id.rmb);
				view.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) view.getTag();
			}

			// 判断添加标签
			if ("1".equals(resultList.get(position).getIsBerserk())) {
				viewHolder.berserk.setVisibility(View.VISIBLE);
			} else {
				viewHolder.berserk.setVisibility(View.GONE);
			}
			if ("0".equals(resultList.get(position).getInvite())) {
				viewHolder.invitation.setVisibility(View.GONE);
			} else if ("1".equals(resultList.get(position).getInvite())) {
				viewHolder.invitation.setVisibility(View.VISIBLE);
			}
			// 抢机会
			if ("0".equals(resultList.get(position).getJoinFlag())) {
				viewHolder.limit_num.setVisibility(View.VISIBLE);
				viewHolder.group_type.setVisibility(View.VISIBLE);
				viewHolder.joined_num.setVisibility(View.VISIBLE);
				viewHolder.make_an_appointment.setVisibility(View.GONE);
				viewHolder.group_type.setImageResource(R.drawable.group_recruit);
				viewHolder.joined_num.setBackgroundResource(R.drawable.group_join_num1);
				viewHolder.joined_num.setTextColor(getResources().getColor(R.color.main_title));
				viewHolder.joined_num.setText(resultList.get(position).getJoinCount());
				// 已抢到
			} else if ("1".equals(resultList.get(position).getJoinFlag())) {
				viewHolder.limit_num.setVisibility(View.VISIBLE);
				viewHolder.group_type.setVisibility(View.VISIBLE);
				viewHolder.joined_num.setVisibility(View.VISIBLE);
				viewHolder.make_an_appointment.setVisibility(View.GONE);
				viewHolder.group_type.setImageResource(R.drawable.group_joined);
				viewHolder.joined_num.setBackgroundResource(R.drawable.group_join_num2);
				viewHolder.joined_num.setTextColor(getResources().getColor(R.color.group_make_an_appointment));
				viewHolder.joined_num.setText(resultList.get(position).getJoinCount());
				// 已参加
			} else if ("2".equals(resultList.get(position).getJoinFlag())) {
				viewHolder.limit_num.setVisibility(View.VISIBLE);
				viewHolder.group_type.setVisibility(View.VISIBLE);
				viewHolder.joined_num.setVisibility(View.VISIBLE);
				viewHolder.make_an_appointment.setVisibility(View.GONE);
				viewHolder.group_type.setImageResource(R.drawable.group_joined);
				viewHolder.joined_num.setBackgroundResource(R.drawable.group_join_num2);
				viewHolder.joined_num.setTextColor(getResources().getColor(R.color.group_make_an_appointment));
				viewHolder.joined_num.setText(resultList.get(position).getJoinCount());
				// 已过期
			} else if ("3".equals(resultList.get(position).getJoinFlag())) {
				viewHolder.limit_num.setVisibility(View.VISIBLE);
				viewHolder.group_type.setVisibility(View.VISIBLE);
				viewHolder.joined_num.setVisibility(View.VISIBLE);
				viewHolder.make_an_appointment.setVisibility(View.GONE);
				viewHolder.group_type.setImageResource(R.drawable.icon_over);
				viewHolder.joined_num.setBackgroundResource(R.drawable.group_join_num4);
				viewHolder.joined_num.setTextColor(getResources().getColor(R.color.group_over));
				viewHolder.joined_num.setText(resultList.get(position).getJoinCount());
				// 报名止
			} else if ("4".equals(resultList.get(position).getJoinFlag())) {
				viewHolder.limit_num.setVisibility(View.VISIBLE);
				viewHolder.group_type.setVisibility(View.VISIBLE);
				viewHolder.joined_num.setVisibility(View.VISIBLE);
				viewHolder.make_an_appointment.setVisibility(View.GONE);
				viewHolder.group_type.setImageResource(R.drawable.group_over);
				viewHolder.joined_num.setBackgroundResource(R.drawable.group_join_num4);
				viewHolder.joined_num.setTextColor(getResources().getColor(R.color.group_over));
				viewHolder.joined_num.setText(resultList.get(position).getJoinCount());
				// 已报满
			} else if ("5".equals(resultList.get(position).getJoinFlag())) {
				viewHolder.limit_num.setVisibility(View.VISIBLE);
				viewHolder.group_type.setVisibility(View.VISIBLE);
				viewHolder.joined_num.setVisibility(View.VISIBLE);
				viewHolder.make_an_appointment.setVisibility(View.GONE);
				viewHolder.group_type.setImageResource(R.drawable.group_full);
				viewHolder.joined_num.setBackgroundResource(R.drawable.group_join_num3);
				viewHolder.joined_num.setTextColor(getResources().getColor(R.color.group_full));
				viewHolder.joined_num.setText(resultList.get(position).getJoinCount());
				// 预约
			} else if ("6".equals(resultList.get(position).getJoinFlag())) {
				viewHolder.limit_num.setVisibility(View.INVISIBLE);
				viewHolder.group_type.setVisibility(View.VISIBLE);
				viewHolder.group_type.setImageResource(R.drawable.group_make_an_appointment);
				viewHolder.joined_num.setVisibility(View.INVISIBLE);
				viewHolder.make_an_appointment.setVisibility(View.VISIBLE);
				// 已预约
			} else if ("7".equals(resultList.get(position).getJoinFlag())) {
				viewHolder.limit_num.setVisibility(View.INVISIBLE);
				viewHolder.group_type.setVisibility(View.VISIBLE);
				viewHolder.group_type.setImageResource(R.drawable.group_make_an_appointment);
				viewHolder.joined_num.setVisibility(View.INVISIBLE);
				viewHolder.make_an_appointment.setVisibility(View.VISIBLE);
			} else {

			}
			Spannable WordtoSpan = new SpannableString("￥" + resultList.get(position).getJoinRmb());
			WordtoSpan.setSpan(new AbsoluteSizeSpan(50), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			viewHolder.rmb.setText(WordtoSpan);

			viewHolder.group_time.setText(resultList.get(position).getTime());

			viewHolder.limit_num.setText("/" + resultList.get(position).getLimitCount());
			// 字体加粗
			TextPaint tp = viewHolder.group_title.getPaint();
			tp.setFakeBoldText(true);
			TextPaint tp2 = viewHolder.feedbackCoin.getPaint();
			tp2.setFakeBoldText(true);
			viewHolder.group_title.setText(resultList.get(position).getTitle());
			viewHolder.feedbackCoin.setText(resultList.get(position).getCoin());
			viewHolder.group_description.setText(resultList.get(position).getSimpleContent());
			// viewHolder.group_description.setText("主办方简介主办方简介主办方简介主办方简介主办方简介主办方简介主办方简介");
			if (!"".equals(resultList.get(position).getLogo())) {
				imageLoader.displayImage(AppConfig.HOST + resultList.get(position).getLogo(), viewHolder.group_img,
						Constants.image_display_options, AppConfig.animateListener);
			} else {
				viewHolder.group_img.setImageResource(R.drawable.ic_stub);
			}

			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(ThemeDetailWaterFallActivity.this, GroupDetailActivity.class);
					intent.putExtra("messageId", resultList.get(position).getMessageId());
					intent.putExtra("position", position);
					ThemeDetailWaterFallActivity.this.startActivityForResult(intent, AppConfig.REQUEST_GROUP);
				}
			});
			return view;
		}

		class ViewHolder {
			TextView group_title, group_time, joined_num, limit_num, limit_text, group_content, feedbackCoin,
					group_description, rmb, make_an_appointment;
			LinearLayout myLayout;
			ImageView group_img, isjoin, activity_img, group_time_img, berserk, invitation, group_type;
			Button group_join, group_discuss;

		}
	}

	@SuppressWarnings("unchecked")
	private void getSeller() {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); // 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "queryCloudSellerNew1")); // 添加要传递的参数
		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		params.add(new BasicNameValuePair("pageNo", pageNo));
		params.add(new BasicNameValuePair("strFlag", "0"));
		params.add(new BasicNameValuePair("keyword", ""));
		params.add(new BasicNameValuePair("pmId", ThemeId));

		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {

				stopProgressDialog();

				Type type = new TypeToken<List<ClubBean>>() {
				}.getType();
				Gson gson = new Gson();
				String result = (String) object;
				Object fromJson = gson.fromJson(result, type);

				try {
					mClubDataList = (List<ClubBean>) fromJson;
				} catch (Exception e) {
					return;
				}

				if (mClubDataList != null) {
					mXListClub.setVisibility(View.VISIBLE);
					if (flgMore) {
						if (mClubDataList.size() == 0) {

							Toast.makeText(getApplicationContext(), "已经加载全部", Toast.LENGTH_SHORT).show();

							if (!"1".equals(pageNo)) {
								pageNo = String.valueOf(Integer.parseInt(pageNo) - 1);
							} else {
								pageNo = "0";
							}
							resetXList();
						} else {
							resultClubList.clear();
							for (ClubBean bean : mClubDataList) {

								resultClubList.add(bean);
							}
							clubDataAdapter.addItemLast(resultClubList);
							resetXList();

						}
					} else {
						if (mClubDataList.size() == 0) {
							Toast.makeText(getApplicationContext(), "已经加载全部", Toast.LENGTH_SHORT).show();
							resultClubList.clear();
							resetXList();
						}
						resultClubList.clear();
						for (ClubBean bean : mClubDataList) {
							resultClubList.add(bean);
						}
						clubDataAdapter.addItemTop(resultClubList);
						clubDataAdapter.notifyDataSetChanged();
						resetXList();
					}

				} else {
					Toast.makeText(getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onFail(String exception) {

				stopProgressDialog();
				Alert.error(ThemeDetailWaterFallActivity.this,"与服务器连接失败，请检查网络");
				resetXList();

			}
		}, ClubBean.class, AppConfig.CLUB).execute(params);
	}

	class clubDataAdapter extends BaseAdapter {

		private Activity activity;
		private LayoutInflater inflater;
		private LinkedList<ClubBean> resultList;

		clubDataAdapter(Activity activity) {

			this.activity = activity;
			inflater = LayoutInflater.from(activity);
			resultList = new LinkedList<ClubBean>();
		}

		public void addItemLast(List<ClubBean> datas) {
			resultList.addAll(datas);
		}

		public void addItemTop(List<ClubBean> datas) {
			resultList.clear();
			for (ClubBean info : datas) {
				resultList.add(info);
			}
		}

		@Override
		public int getCount() {

			return resultList.size();
		}

		@Override
		public Object getItem(int arg0) {

			return resultList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {

			return arg0;
		}

		@Override
		public View getView(final int position, View view, ViewGroup arg2) {
			View itemView = null;

			if (view == null) {
				// viewHolder = new ViewHolder();
				view = inflater.inflate(R.layout.club_item, null);
				itemView = view;
				view.setTag(itemView);

				// view.setTag(viewHolder);
			} else {
				itemView = (View) view.getTag();
				// viewHolder = (ViewHolder) view.getTag();
			}
			ImageView club_img = (ImageView) itemView.findViewById(R.id.club_img);
			ImageView watermark1 = (ImageView) itemView.findViewById(R.id.watermark1);
			ImageView watermark2 = (ImageView) itemView.findViewById(R.id.watermark2);
			ImageView watermark3 = (ImageView) itemView.findViewById(R.id.watermark3);
			ImageView watermark4 = (ImageView) itemView.findViewById(R.id.watermark4);
			ImageView type = (ImageView) itemView.findViewById(R.id.type);
			TextView club_title = (TextView) itemView.findViewById(R.id.club_title);
			TextView publicCount = (TextView) itemView.findViewById(R.id.publicCount);
			TextView friendCount = (TextView) itemView.findViewById(R.id.friendCount);
			final Button attention = (Button) itemView.findViewById(R.id.attention);
			TextView friendCount_title = (TextView) itemView.findViewById(R.id.friendCount_title);
			TextView club_recomand_name = (TextView) itemView.findViewById(R.id.club_recomand_name);
			LinearLayout watermark_bg = (LinearLayout) itemView.findViewById(R.id.watermark_bg);

			club_title.setText(resultList.get(position).getSectionName());

			publicCount.setText(resultList.get(position).getPublicCount());

			friendCount.setText(resultList.get(position).getFriendCount());
			if (!resultList.get(position).getRecommendUserNickname().equals("")) {
				club_recomand_name.setText("(推荐人: " + resultList.get(position).getRecommendUserNickname() + ")");
			} else {
				club_recomand_name.setText("");
			}

			friendCount.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(activity, NewHomeActivity.class);
					intent.putExtra("sectionId", resultList.get(position).getSectionId());
					intent.putExtra("sectionName", resultList.get(position).getSectionName());

					startActivity(intent);
				}
			});
			friendCount_title.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(activity, NewHomeActivity.class);
					intent.putExtra("sectionId", resultList.get(position).getSectionId());
					intent.putExtra("sectionName", resultList.get(position).getSectionName());

					startActivity(intent);
				}
			});
			if ("0".equals(resultList.get(position).getScreenFlag())) {
				attention.setBackgroundResource(R.drawable.club_unfollow);

			} else {
				attention.setBackgroundResource(R.drawable.club_follow);

			}
			attention.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
				}
			});

			if ("0".equals(resultList.get(position).getType())) {
				type.setImageResource(R.drawable.club_authority);
			} else if ("2".equals(resultList.get(position).getType())) {
				type.setImageResource(R.drawable.club_recommend);
			}
			if (!"".equals(resultList.get(position).getLogo())) {
				club_img.setVisibility(View.VISIBLE);
				imageLoader.displayImage(AppConfig.HOST + resultList.get(position).getLogo(), club_img,
						Constants.image_display_options, AppConfig.animateListener);
			} else {
				club_img.setImageResource(R.drawable.club_img4);
			}

			view.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {

					if ("0".equals(resultList.get(position).getType())) {
						Intent intent = new Intent();
						intent.setClass(activity, ClubDetailActivity.class);
						intent.putExtra("sectionId", resultList.get(position).getSectionId());
						intent.putExtra("position", position);
						activity.startActivityForResult(intent, AppConfig.REQUEST_CLUB);
					} else if ("2".equals(resultList.get(position).getType())) {
						Intent intent = new Intent();
						intent.setClass(activity, RecommendClubDetailActivity.class);
						intent.putExtra("sectionId", resultList.get(position).getSectionId());
						intent.putExtra("position", position);
						activity.startActivityForResult(intent, AppConfig.REQUEST_CLUB);
					}

				}
			});

			List<String> list = Utils.stringToList(resultList.get(position).getWmString(), ", ");
			if (list.size() <= 4 && list.size() > 0) {
				watermark_bg.setVisibility(View.VISIBLE);
				switch (list.size()) {
				case 0:
					watermark1.setVisibility(View.GONE);
					watermark2.setVisibility(View.GONE);
					watermark3.setVisibility(View.GONE);
					watermark4.setVisibility(View.GONE);
					break;
				case 1:
					watermark1.setVisibility(View.VISIBLE);
					watermark2.setVisibility(View.GONE);
					watermark3.setVisibility(View.GONE);
					watermark4.setVisibility(View.GONE);
					imageLoader.displayImage(AppConfig.HOST + list.get(0), watermark1, Constants.image_display_options,
							AppConfig.animateListener);
					break;
				case 2:
					watermark1.setVisibility(View.VISIBLE);
					watermark2.setVisibility(View.VISIBLE);
					watermark3.setVisibility(View.GONE);
					watermark4.setVisibility(View.GONE);
					imageLoader.displayImage(AppConfig.HOST + list.get(0), watermark1, Constants.image_display_options,
							AppConfig.animateListener);
					imageLoader.displayImage(AppConfig.HOST + list.get(1), watermark2, Constants.image_display_options,
							AppConfig.animateListener);
					break;
				case 3:
					watermark1.setVisibility(View.VISIBLE);
					watermark2.setVisibility(View.VISIBLE);
					watermark3.setVisibility(View.VISIBLE);
					watermark4.setVisibility(View.GONE);
					imageLoader.displayImage(AppConfig.HOST + list.get(0), watermark1, Constants.image_display_options,
							AppConfig.animateListener);
					imageLoader.displayImage(AppConfig.HOST + list.get(1), watermark2, Constants.image_display_options,
							AppConfig.animateListener);
					imageLoader.displayImage(AppConfig.HOST + list.get(2), watermark3, Constants.image_display_options,
							AppConfig.animateListener);
					break;
				case 4:
					watermark1.setVisibility(View.VISIBLE);
					watermark2.setVisibility(View.VISIBLE);
					watermark3.setVisibility(View.VISIBLE);
					watermark4.setVisibility(View.VISIBLE);
					imageLoader.displayImage(AppConfig.HOST + list.get(0), watermark1, Constants.image_display_options,
							AppConfig.animateListener);
					imageLoader.displayImage(AppConfig.HOST + list.get(1), watermark2, Constants.image_display_options,
							AppConfig.animateListener);
					imageLoader.displayImage(AppConfig.HOST + list.get(2), watermark3, Constants.image_display_options,
							AppConfig.animateListener);
					imageLoader.displayImage(AppConfig.HOST + list.get(3), watermark4, Constants.image_display_options,
							AppConfig.animateListener);
					break;

				default:
					break;
				}
			} else if (list.size() > 4) {
				watermark_bg.setVisibility(View.VISIBLE);
				watermark1.setVisibility(View.VISIBLE);
				watermark2.setVisibility(View.VISIBLE);
				watermark3.setVisibility(View.VISIBLE);
				watermark4.setVisibility(View.VISIBLE);
				imageLoader.displayImage(AppConfig.HOST + list.get(0), watermark1, Constants.image_display_options,
						AppConfig.animateListener);
				imageLoader.displayImage(AppConfig.HOST + list.get(1), watermark2, Constants.image_display_options,
						AppConfig.animateListener);
				imageLoader.displayImage(AppConfig.HOST + list.get(2), watermark3, Constants.image_display_options,
						AppConfig.animateListener);
				imageLoader.displayImage(AppConfig.HOST + list.get(3), watermark4, Constants.image_display_options,
						AppConfig.animateListener);

			} else {
				watermark_bg.setVisibility(View.GONE);
			}
			return view;
		}

		class ViewHolder {

			TextView club_title, description, member_number;
			LinearLayout keyword_layout;
			ImageView club_img;
			Button btn_focus;
		}
	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
			progressDialog.setMessage("正在加载...");
		}

		progressDialog.show();
	}

	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	@Override
	public void onRefresh() {
		resultList.clear();
		flgMore = false;
		pageNo = "0";
		if (mAdapterView.getVisibility() == View.VISIBLE) {
			getThemeContent();

		} else if (mXList.getVisibility() == View.VISIBLE) {
			getThemeData();
		} else if (mXListClub.getVisibility() == View.VISIBLE) {
			getSeller();
		} else {

		}
		resetXList();

	}

	@Override
	public void onLoadMore() {
		flgMore = true;
		pageNo = String.valueOf(Integer.parseInt(pageNo) + 1);
		if (mAdapterView.getVisibility() == View.VISIBLE) {
			getThemeContent();

		} else if (mXList.getVisibility() == View.VISIBLE) {
			getThemeData();
		} else if (mXListClub.getVisibility() == View.VISIBLE) {
			getSeller();
		} else {

		}

		resetXList();

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_left:
			finish();
			break;
		case R.id.btn_kanfxiang:
			btn_kanfxiang.setVisibility(View.GONE);
			btn_qdli.setVisibility(View.VISIBLE);
			order_num_bg.setBackgroundResource(R.color.main_title);
			startProgressDialog();
			mAdapterView.setVisibility(View.GONE);
			getSeller();
			btn_qdli.setOnClickListener(this);
			break;
		case R.id.btn_qdli:
			btn_qdli.setVisibility(View.GONE);
			btn_kanfxiang.setVisibility(View.VISIBLE);
			mXListClub.setVisibility(View.GONE);
			mAdapterView.setVisibility(View.VISIBLE);
			btn_kanfxiang.setOnClickListener(this);
			break;
		case R.id.btn_kfxiang:
			btn_kfxiang.setVisibility(View.GONE);
			btn_qjhui.setVisibility(View.VISIBLE);
			order_num_bg.setBackgroundResource(R.color.main_title);
			startProgressDialog();
			mAdapterView.setVisibility(View.GONE);
			getThemeData();
			btn_qjhui.setOnClickListener(this);
			break;
		case R.id.btn_qjhui:
			btn_qjhui.setVisibility(View.GONE);
			btn_kfxiang.setVisibility(View.VISIBLE);
			mXList.setVisibility(View.GONE);
			mAdapterView.setVisibility(View.VISIBLE);
			btn_kfxiang.setOnClickListener(this);
			break;
		case R.id.joined:
			Intent i = new Intent(ThemeDetailWaterFallActivity.this, AddMyShareActivity.class);
			i.putExtra("isRelate", isRelate);
			i.putExtra("ThemeId", ThemeId);
			startActivity(i);

			break;
		default:
			break;
		}

	}
}
